package javaEEBasic.day03;

import java.util.Arrays;
import java.util.Random;

public class Test01 {
    public static void main(String[] args) {
        System.out.println(generateVerifyCode(8));
        System.out.println(encryptCodeNum(1));
        System.out.println(Arrays.toString(randomArr(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})));
    }


    //数组随机打乱顺序
    //全排列问题
    public static int[] randomArr(int[] arr) {
        if (arr == null || arr.length == 0) {
            throw new RuntimeException("参数错误");
        }
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            int index = random.nextInt(arr.length);
            int temp = arr[i];
            arr[i] = arr[index];
            arr[index] = temp;
        }
        return arr;
    }

    //生成指定位数的验证吗，一半字母(大小写)加一半数字
    public static String generateVerifyCode(int n) {
        Random random = new Random();
        int numCount = n / 2;
        int charCount = n - numCount;
        StringBuffer code = new StringBuffer();
        for (int i = 0; i < numCount; i++) {
            code.append(random.nextInt(10));
        }
        for (int i = 0; i < charCount; i++) {
            int randomType = random.nextInt(2);
            if (randomType == 0) {
                //A-Z
                code.append((char) (random.nextInt(26) + 65));
            } else {
                //a-z
                code.append((char) (random.nextInt(26) + 97));
            }
        }
        //随机打乱顺序
        char[] chars = code.toString().toCharArray();
        for (int i = 0; i < chars.length; i++) {
            int randomIndex = random.nextInt(chars.length);
            char temp = chars[i];
            chars[i] = chars[randomIndex];
            chars[randomIndex] = temp;
        }
        String codeStr = new String(chars);
        return codeStr;
    }

    //数字加密 :每位数字+5再对0求余，最后反转
    public static String encryptCodeNum(int number) {

        String src = number + "";
        int[] numCode = new int[src.length()];
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < src.length(); i++) {
            numCode[i] = src.charAt(i) - '0';
            numCode[i] += 5;
            numCode[i] %= 10;
            result.append(numCode[i]);
        }
        result.reverse();
        return result.toString();
    }

    //随机生成双色球 6红1蓝
    public static void test05() {
        //理解，[1,33],[1,16]
        Random random = new Random();
        int redNum = 0;
        StringBuilder red = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            redNum = random.nextInt(33) + 1;
            red.append(redNum + "-");
            if (i == 5) {
                //删除最后一个-
                red.deleteCharAt(red.length() - 1);
            }
        }
        int blueNum = random.nextInt(16) + 1;
        String result = red.append(" ").append(blueNum).toString();
        System.out.println(result);
    }
}
